package com.scisoft.scan.control.stock;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.chrisbanes.photoview.PhotoView;
import com.scisoft.scan.data.stock.StockInfoBean;
import com.scisoft.scan.data.stock.StockInfoPictureUrlBean;
import com.scisoft.scan.dy.R;
import com.scisoft.scan.util.CommonUtil;
import com.scisoft.scan.ws.WebServiceHelper_Stock;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class StockPictureEditActivity extends Activity {
    private ImageView iv_Back; //返回按钮
    private TextView tv_submit;//提交按钮
    private PhotoView iv_data;//图片
    private StockInfoBean stockInfoBean;
    private LinearLayout layout_picture;
    private List<StockInfoPictureUrlBean> stockPictureList;
    private int currentSelection = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//remove title bar  即隐藏标题栏
        setContentView(R.layout.activity_stock_picture_edit);
        stockPictureList = new ArrayList<>();
        //参数传递
        Intent intent = getIntent();
        //配件
        stockInfoBean = (StockInfoBean) intent.getSerializableExtra("stockInfoBean");
        //返回
        iv_Back = findViewById(R.id.iv_back);
        iv_Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //提交
        tv_submit = findViewById(R.id.tv_submit);
        tv_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentSelection >= 0 && currentSelection < stockPictureList.size()) {
                    CommonUtil.showPress(StockPictureEditActivity.this);
                    SubmitTask submitTask = new SubmitTask(StockPictureEditActivity.this);
                    submitTask.execute();
                } else {
                    Toast.makeText(getApplicationContext(), "请选择一张图片后再删除", Toast.LENGTH_LONG).show();
                }


            }
        });
        //图片列表
        layout_picture = findViewById(R.id.layout_picture);
        iv_data = findViewById(R.id.iv_data);

        CommonUtil.showPress(StockPictureEditActivity.this);
        QueryTask queryTask = new QueryTask(StockPictureEditActivity.this);
        queryTask.execute();

    }

    //绑定数据
    private void BindData(Context context) {
        //数据
        if (stockPictureList != null) {
            DisplayMetrics displayMetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
            float density = displayMetrics.density;
            layout_picture.removeAllViews();
            for (int i = 0; i < stockPictureList.size(); i++) {
                StockInfoPictureUrlBean pictureUrlBean = stockPictureList.get(i);
                String url = pictureUrlBean.partPictureUrl;
                final ImageView view = new ImageView(StockPictureEditActivity.this);
                view.setId(i);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int) (48 * density), (int) (48 * density));
                params.leftMargin = 5;
                params.bottomMargin = 5;
                params.topMargin = 5;
                view.setLayoutParams(params);
                view.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        currentSelection = v.getId();
                        SetImageSelection(currentSelection);
                    }
                });
                Picasso.with(StockPictureEditActivity.this).load(url).into(view);
                layout_picture.addView(view);
            }
            //默认选择第一项
            if (stockPictureList.size() > 0) {
                currentSelection = 0;
                SetImageSelection(currentSelection);
            }
        }
    }

    //设置选中
    private void SetImageSelection(int index) {
        for (int i = 0; i < layout_picture.getChildCount(); i++) {
            ImageView view = (ImageView) layout_picture.getChildAt(i);
            if (view.getId() == index) {
                view.setBackground(getResources().getDrawable(R.drawable.edit_bg_stat_red));
                if (index >= 0 && index < stockPictureList.size()) {
                    StockInfoPictureUrlBean temp = stockPictureList.get(i);
                    if (temp.partPictureUrl != null && !temp.partPictureUrl.equals("")) {
                        Picasso.with(StockPictureEditActivity.this).load(temp.partPictureUrl).into(iv_data);
                    } else {
                        iv_data.setImageDrawable(getResources().getDrawable(R.drawable.ic_noimage));
                    }
                }
            } else {
                view.setBackground(null);
            }
        }
    }

    //查询库存服务
    class QueryTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";

        QueryTask(Context context) {
            super();
            mcontext = context;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                stockPictureList = WebServiceHelper_Stock.STOCK_10(stockInfoBean.partId);
                msg = "成功";
            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            if (result.contains("成功")) {
                BindData(mcontext);
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }

    //查询库存服务
    class SubmitTask extends AsyncTask<String, Integer, String> {
        private Context mcontext;
        private String msg = "";

        SubmitTask(Context context) {
            super();
            mcontext = context;
        }

        @Override
        protected String doInBackground(String... params) {
            //查询
            try {
                msg = WebServiceHelper_Stock.STOCK_09(stockPictureList.get(currentSelection).partPictureId);
                if (msg.contains("成功")) {
                    stockPictureList = WebServiceHelper_Stock.STOCK_10(stockInfoBean.partId);
                }
            } catch (Exception e) {
                msg = e.getMessage();
                e.printStackTrace();
            }
            //将结果返回给onPostExecute方法
            return msg;
        }

        @Override
        //此方法可以在主线程改变UI
        protected void onPostExecute(String result) {
            CommonUtil.hidePress();
            if (msg.contains("成功")) {
                BindData(mcontext);
            } else {
                Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
            }
        }
    }
}
